
import { useObserver } from 'mobx-react-lite'
import stores from '@/stores';
import { Route, Routes } from 'react-router-dom';
import * as Views from '@/views';
import request from "@shared/apis/request";
import { Toast } from '@/components/base';

export default function App() {
  const token = useObserver(() => stores.token);
  return (
    token
      ? (
        <Routes>
          <Route path='*' element={<Views.BasicLayout />} /> 
        </Routes>
      ) : (
        <Routes>
          <Route path='*' element={<Views.Logon />} />
        </Routes>
      )
  )
}

request.interceptors.request.use(
  (config) => {
    if (stores.token) {
      config.headers.Authorization = `Bearer ${stores.token}`
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
)

request.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    // 检查是否设置了 silent 参数
    if (!error.config?.silent) {
      // 如果是取消请求，不显示错误提示
      if (error.name === 'CanceledError' || error.code === 'ERR_CANCELED') {
        return Promise.reject(error)
      }
      const content = error.response?.data?.message || '请求失败';
      Toast.show({ icon: 'fail', content })
    }
    return Promise.reject(error)
  }
)